<template>
	<view>
		<u-swipe-action>
			<u-swipe-action-item :options="options2" v-for="item in sourcelist" :key="item.id" @click="toPages"
				:name="item.id">
				<view class="swipe-action u-border-top u-border-bottom">
					<view class="swipe-action__content">
						<text class="swipe-action__content__text">{{item.name}}</text>
					</view>
				</view>
			</u-swipe-action-item>
		</u-swipe-action>
		<u-button type="success" text="添加客户来源" color="#19BC9C" :customStyle="btnStyle" @click="toAddPage"></u-button>
		<!-- 消息提示 -->
		<u-toast ref="uToast"></u-toast>
		<!-- 确认框 -->
		<u-modal :show="show" :content='content' :showCancelButton="true" @cancel="show = false"
			@confirm="confirm"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				options2: [{
					text: '编辑',
					style: {
						backgroundColor: '#3c9cff'
					}
				}, {
					text: '删除',
					style: {
						backgroundColor: '#f56c6c'
					}
				}],
				btnStyle: {
					position: 'fixed',
					bottom: '0px',
					width: '100%',
					bottom: '0px'
				},
				sourcelist: [],
				show: false,
				content: '确认是否删除当前分类名称？',
				sourceid: 0,
				LoginUser:{}
			};
		},
		onShow() {
			this.$u.auth.check()
			this.LoginUser = uni.getStorageSync('LoginUser') ? uni.getStorageSync('LoginUser') : {};
			this.getSourceData()
		},
		methods: {
			async getSourceData() {
				let result = await this.$u.api.business.SourceIndex()
				if (result.code == 1) {
					this.sourcelist = result.data
				}
			},
			toAddPage() {
				uni.navigateTo({
					url: '/pages/business/source/add'
				})
			},
			toPages(item) {
				if (item.index == 0) {
					uni.navigateTo({
						url: '/pages/business/source/edit?sourceid=' + item.name
					})
				} else {
					this.show = true
					this.sourceid = item.name
				}
			},
			async confirm() {
				this.show = false
				let result = await this.$u.api.business.SourceDel({
					sourceid: this.sourceid
				})
				if (result.code == 1) {
					this.$refs.uToast.show({
						type: 'success',
						message: result.msg,
					})
					this.getSourceData()
				} else {
					this.$refs.uToast.show({
						type: 'error',
						message: result.msg,
					})
				}
			}
		}
	};
</script>

<style lang="scss">
	.u-page {
		padding: 0;
	}

	.u-demo-block__title {
		padding: 10px 0 2px 15px;
	}

	.swipe-action {
		&__content {
			padding: 25rpx 0;

			&__text {
				font-size: 15px;
				color: $u-main-color;
				padding-left: 30rpx;
			}
		}
	}
</style>